// CListCtrlMoreNotifies.cpp : Implementierungsdatei
//

#include "stdafx.h"
#include "Charplan.h"
#include "CListCtrlMoreNotifies.h"
#include ".\clistctrlmorenotifies.h"


// CListCtrlMoreNotifies

IMPLEMENT_DYNAMIC(CListCtrlMoreNotifies, CListCtrl)
CListCtrlMoreNotifies::CListCtrlMoreNotifies()
{
}

CListCtrlMoreNotifies::~CListCtrlMoreNotifies()
{
}


BEGIN_MESSAGE_MAP(CListCtrlMoreNotifies, CListCtrl)
    ON_NOTIFY(HDN_BEGINTRACKA, 0, OnHdnBegintrack)
    ON_NOTIFY(HDN_BEGINTRACKW, 0, OnHdnBegintrack)
    ON_NOTIFY(HDN_ENDTRACKA, 0, OnHdnEndtrack)
    ON_NOTIFY(HDN_ENDTRACKW, 0, OnHdnEndtrack)
    ON_NOTIFY(HDN_TRACKA, 0, OnHdnTrack)
    ON_NOTIFY(HDN_TRACKW, 0, OnHdnTrack)
    ON_NOTIFY(HDN_TRACK, 0, OnHdnTrack)
    ON_WM_VSCROLL()
    ON_WM_MOUSEMOVE()
    ON_WM_HSCROLL()
    ON_WM_MOUSEWHEEL()
    ON_WM_KEYDOWN()
    ON_WM_ERASEBKGND()
    ON_WM_SETFOCUS()
END_MESSAGE_MAP()



void CListCtrlMoreNotifies::OnHdnBegintrack(NMHDR *pNMHDR, LRESULT *pResult)
{
    pNMHDR->code = HDN_BEGINTRACK;
    pNMHDR->hwndFrom = GetSafeHwnd();
    pNMHDR->idFrom = GetDlgCtrlID();
    GetParent()->SendMessage(WM_NOTIFY,0, (LPARAM)pNMHDR);

    *pResult = 0;
}


void CListCtrlMoreNotifies::OnHdnEndtrack(NMHDR *pNMHDR, LRESULT *pResult)
{
    pNMHDR->code = HDN_ENDTRACK;
    pNMHDR->hwndFrom = GetSafeHwnd();
    pNMHDR->idFrom = GetDlgCtrlID();
    GetParent()->SendMessage(WM_NOTIFY,0, (LPARAM)pNMHDR);

    *pResult = 0;
}

void CListCtrlMoreNotifies::OnHdnTrack(NMHDR *pNMHDR, LRESULT *pResult)
{
    pNMHDR->code = HDN_TRACK;
    pNMHDR->hwndFrom = GetSafeHwnd();
    pNMHDR->idFrom = GetDlgCtrlID();
    GetParent()->SendMessage(WM_NOTIFY,0, (LPARAM)pNMHDR);

    *pResult = 0;
}


void CListCtrlMoreNotifies::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    CListCtrl::OnVScroll(nSBCode, nPos, pScrollBar);

    NMHDR nm;
    nm.code = LVN_CELL_POS_CHANGED;
    nm.hwndFrom = GetSafeHwnd();
    nm.idFrom = GetDlgCtrlID();
    GetParent()->SendMessage(WM_NOTIFY,0, (LPARAM)&nm);

}

void CListCtrlMoreNotifies::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
    CListCtrl::OnHScroll(nSBCode, nPos, pScrollBar);

    NMHDR nm;
    nm.code = LVN_CELL_POS_CHANGED;
    nm.hwndFrom = GetSafeHwnd();
    nm.idFrom = GetDlgCtrlID();
    GetParent()->SendMessage(WM_NOTIFY,0, (LPARAM)&nm);
}



void CListCtrlMoreNotifies::OnMouseMove(UINT nFlags, CPoint point)
{
    UINT uFlags;
    int nItem = HitTest(point, &uFlags);

    if (nItem!=-1)
    {
        GetParent()->SendMessage(LVN_MOUSEMOVE,
                                 MAKEWPARAM(nItem, GetDlgCtrlID()),
								 MAKELPARAM(point.x,nFlags));
    }

    CListCtrl::OnMouseMove(nFlags, point);
}



BOOL CListCtrlMoreNotifies::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
    BOOL b_res = CListCtrl::OnMouseWheel(nFlags, zDelta, pt);

    NMHDR nm;
    nm.code = LVN_CELL_POS_CHANGED;
    nm.hwndFrom = GetSafeHwnd();
    nm.idFrom = GetDlgCtrlID();
    GetParent()->SendMessage(WM_NOTIFY,0, (LPARAM)&nm);

    return b_res;
}


void CListCtrlMoreNotifies::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
    CListCtrl::OnKeyDown(nChar, nRepCnt, nFlags);

    switch (nChar)
    {
        case VK_UP:
        case VK_DOWN:
        case VK_LEFT:
        case VK_RIGHT:
        case VK_HOME:
        case VK_END:
        case VK_PRIOR:
        case VK_NEXT:
        {
            NMHDR nm;
            nm.code = LVN_CELL_POS_CHANGED;
            nm.hwndFrom = GetSafeHwnd();
            nm.idFrom = GetDlgCtrlID();
            GetParent()->SendMessage(WM_NOTIFY,0, (LPARAM)&nm);
        }
        break;
    }
}


BOOL CListCtrlMoreNotifies::OnEraseBkgnd(CDC* pDC)
{
    // only clear not occiped area
    CRect r,r1,r2;

    int x=0;
    CHeaderCtrl* head = GetHeaderCtrl();
    for (int i = 0;i<head->GetItemCount();++i)
    {
        if (head->GetItemRect(i,r))
		{
			if (r.right>x) x=r.right;
		}
    }

    GetClientRect(r1);
    r2=r1;

    // right
	r1.left = min(x,r1.right);
    pDC->FillSolidRect(r1,GetBkColor());

    // bottom
    GetItemRect(GetItemCount()-1,&r,LVIR_BOUNDS);
    r2.top = r.bottom;
    pDC->FillSolidRect(r2,GetBkColor());

    return FALSE;
}


void CListCtrlMoreNotifies::OnSetFocus(CWnd* pOldWnd)
{
    CListCtrl::OnSetFocus(pOldWnd);
    NMHDR nm;
    nm.code = LVN_GOT_FOCUS;
    nm.hwndFrom = GetSafeHwnd();
    nm.idFrom = GetDlgCtrlID();
    GetParent()->SendMessage(WM_NOTIFY,0, (LPARAM)&nm);
}

